﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace MergeBitmap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            foreach (var path in GetChildFolderPath())
            {
                GetRect(path);
                MergerImg(path, path.Substring(path.LastIndexOf('\\')+1));
            }
            MessageBox.Show("success!");
        }

        int _width=300,_height=300;
        int num_x = 0,num_y=0;
        ImageFormat format;
        public void GetRect(string filePath)
        {
            for(int i=0;i<100;i++)
            {
                if (!File.Exists(filePath + "\\" + i.ToString() + "_0.jpg"))
                {
                    num_x = i - 1; break;
                }
            }
            for(int i=0;i<100;i++)
            {
                if (!File.Exists(filePath + "\\0_" + i.ToString() + ".jpg"))
                { num_y = i - 1; ; break; }
            }
        }
        string _folderPath = "E:\\new\\Scene";
        string _targetPath = "E:\\new\\Scene1";
        public string[] GetChildFolderPath()
        {
            return Directory.GetDirectories(_folderPath);
        }
        public string GetFilePath(string folderPath, int x,int y)
        {
            return folderPath + "\\" + x.ToString() + "_" + y.ToString() + ".jpg";
        }

        public bool MergerImg(string folderPath, string name)
        {
                EncoderParameter p;
                EncoderParameters ps;
                ps = new EncoderParameters(1);
                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                ps.Param[0] = p;
                //创建要显示的图片对象,根据参数的个数设置宽度
                Bitmap mergeImage = new Bitmap(_width*num_x, _height*num_y);
                Graphics g = Graphics.FromImage(mergeImage);
                // 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);
                int StartX = 0;
                int StartY = 0;
                for (int j = 0; j < num_y; j++)
                {
                    for (int i = 0; i < num_x; i++)
                    {
                        g.DrawImage(Image.FromFile(GetFilePath(folderPath,i,j)), StartX, StartY, _width, _height);
                        StartX += _width;
                    }
                    StartX = 0;
                    StartY += _height;
                }
                //保存图象   
                mergeImage.Save(_targetPath + "\\" + name+".jpg", ImageFormat.Jpeg);
                g.Dispose();
            return true;
        }

        private ImageCodecInfo GetCodecInfo(string str)
        {
            string ext = str;
            string mimeType = "";
            switch (ext.ToLower())
            {
                case "jpe":
                case "jpg":
                case "jpeg":
                    mimeType = "image/jpeg";
                    break;
                case "bmp":
                    mimeType = "image/bmp";
                    break;
                case "png":
                    mimeType = "image/png";
                    break;
                case "tif":
                case "tiff":
                    mimeType = "image/tiff";
                    break;
                default:
                    mimeType = "image/jpeg";
                    break;
            }
            ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo ici in CodecInfo)
            {
                if (ici.MimeType == mimeType) return ici;
            }
            return null;
        }
    }
}
